package com.walker.elk.netty.chat;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import com.walker.elk.netty.chat.client.WalkerChatClientChannelInitializer;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

public class WalkerChatClient {
	
	public static void main(String[] args) {
		//客户端只需要一个
		EventLoopGroup eventLoopGroup = new NioEventLoopGroup();  //接受请求

		try {
			Bootstrap bootstrap = new Bootstrap();
			bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class).            //这里告诉Channel如何接收新的连接
			handler(new WalkerChatClientChannelInitializer());                              //指定初始化channel对象
			Channel channel = bootstrap.connect("localhost", 8899).sync().channel();   //连接到服务器
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
			while(true){
				channel.writeAndFlush(bufferedReader.readLine() + "\n");
			}
		} catch (Exception e) {
			eventLoopGroup.shutdownGracefully();
			e.printStackTrace();
		}
	}
}
